package com.example.springbasic.controller;

import com.example.springbasic.dto.UserDto;
import com.example.springbasic.dto.UserXmlDto;
import com.example.springbasic.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;


@RestController
public class BasePostController {
    @Autowired
    @Qualifier("userp")
    UserService userService;
    // 等于UserService userService = new UserService()这样一个实现类
    @PostMapping(value = "/login",produces = "application/json")
    String login(@RequestBody UserDto userDto){
//        if(userDto.getUsername().equals("limou") && userDto.getPassword().equals("123456")){
//            return "用户登陆成功！用户名：{"+userDto.getUsername()+"},密码为：{"+userDto.getPassword()+"}";
//        }
//        else {
//            return "用户登陆失败！用户名：{"+userDto.getUsername()+"},密码为：{"+userDto.getPassword()+"}";
//        }
        return userService.login(userDto);
    }

    //http://localhost:8080/{module}/register?desc={desc}&age={age}
    @PostMapping(value = "/{module}/register", produces = "application/json")
    String registerAndParma(@RequestBody UserDto userDto,
                            @RequestParam String module,
                            @PathVariable String desc,
                            @PathVariable String age){
        return userDto.getUsername()+"注册成功！密码为："+userDto.getPassword()+"\n注册模块为"+module+"\n年龄为"+age+"\n其他描述"+desc;
    }

    @PostMapping(value = "/{module}/registerxml", produces = MediaType.APPLICATION_ATOM_XML_VALUE)
    String registerWithXmlAndParma(@RequestBody UserXmlDto userXmlDto,
                                   @RequestParam String module,
                                   @PathVariable String desc,
                                   @PathVariable String age) {
        return userXmlDto.getUname()+"注册成功！密码为："+userXmlDto.getPwd()+"\n注册模块为"+module+"\n年龄为"+age+"\n其他描述"+desc;
    }
}
